require "NodeManager"

NodeID={HeroTableBK=3001, HeroBK=3002, HeroImg=3003, EquipTableBK=3004,
        BootsBK=3011, HelmetBK=3012, ManteauBK=3013, UniformBK=3014, WeaponBK=3015, Weapon2BK=3016} 


EquipHeroScene={}
EquipHeroScene.heros = {"hero13", "hero14", "hero15", "hero17", "hero18", "hero19"}
EquipHeroScene.equips = {"boots", "helmet", "manteau", "uniform", "weapon", "weapon2"}

EquipHeroScene.getNode = function()
  EquipHeroScene.node = CCBReader:load("EquipHeroUI.ccbi")  
  EquipHeroScene.node:registerScriptTouchHandler(onTouchLayer)
  EquipHeroScene.node:setTouchEnabled(true)
  print("EquipHeroScene.getNode", EquipHeroScene.node:isTouchEnabled())
  EquipHeroScene.initTable()
  
  local p1 = EquipHeroScene.node:getChildByTag(NodeID.BootsBK)
  local p2 = EquipHeroScene.node:getChildByTag(NodeID.HelmetBK)
  local p3 = EquipHeroScene.node:getChildByTag(NodeID.ManteauBK)
  local p4 = EquipHeroScene.node:getChildByTag(NodeID.UniformBK)
  local p5 = EquipHeroScene.node:getChildByTag(NodeID.WeaponBK)
  local p6 = EquipHeroScene.node:getChildByTag(NodeID.Weapon2BK)
  EquipHeroScene.equipBKs={p1, p2, p3, p4, p5, p6}
  return EquipHeroScene.node
end


EquipHeroScene.getScene = function()
  local sc = CCScene:create()
  sc:addChild(EquipHeroScene.getNode())
  print("WarScene.getScene", sc)
  return sc
end

EquipHeroScene.initTable = function()
  
  --英雄table
  local heroTableBK = EquipHeroScene.node:getChildByTag(NodeID.HeroTableBK)
  local bksize=heroTableBK:getContentSize()
  local items = CCArray:create()
  local count = #EquipHeroScene.heros
  for i=1,count do
    local name = EquipHeroScene.heros[i]
    local layer = CCLayer:create()
    local sp = CCSprite:create("UI92.png")
    local size = sp:getTexture():getContentSizeInPixels()
    size = CCSizeMake(size.width+20, size.height+10)
    layer:setContentSize(size)
    local button=CCButton:createWithFileName("onSelectHero","UI92.png","UI92.png","UI92.png") 
    local herosp=CCSprite:create(string.format("%shead.png",name))
    button:setPosition(size.width/2, size.height/2)
    herosp:setPosition(size.width/2, size.height/2)
    layer:addChild(button)
    layer:addChild(herosp)
    items:addObject(layer)
    NodeManager:addObject(name, button)
  end
  local tableview = CCTableView:create(1, CCSizeMake(bksize.width-50, bksize.height-20), items)
  local x, y = ActionUtil.leftBottomPoint(heroTableBK)
  tableview:setPosition(x+25, y+10)
  EquipHeroScene.node:addChild(tableview)  

  --装备table
  local equipTableBK = EquipHeroScene.node:getChildByTag(NodeID.EquipTableBK)
  local bksize=equipTableBK:getContentSize()
  local items = CCArray:create()
  local count = #EquipHeroScene.equips
  for i=1,count do
    local name = EquipHeroScene.equips[i]
    local layer = CCLayer:create()
    local sp = CCSprite:create("UI9.png")
    local size = sp:getTexture():getContentSizeInPixels()
    size = CCSizeMake(size.width+20, size.height+10)
    layer:setContentSize(size)
    local button=CCButton:createWithFileName("onSelectEquip","UI9.png","UI9.png","UI9.png") 
    local colorbk=CCSprite:create("UI114.png")
    local equipsp=CCSprite:create(string.format("equip_1_%s_1.png",name))
    button:setPosition(size.width/2, size.height/2)
    colorbk:setPosition(size.width/2, size.height/2)
    equipsp:setPosition(size.width/2, size.height/2)
    layer:addChild(button)
    layer:addChild(colorbk)
    layer:addChild(equipsp)
    items:addObject(layer)
    NodeManager:addObject(name, button)
  end
  local tableview = CCTableView:create(1, CCSizeMake(bksize.width-100, bksize.height-20), items)
  local x, y = ActionUtil.leftBottomPoint(equipTableBK)
  tableview:setPosition(x+50, y+10)
  EquipHeroScene.node:addChild(tableview)  
end

EquipHeroScene.checkMoveIn = function(x,y)
  --print("EquipHeroScene.checkMoveIn", #EquipHeroScene.equipBKs)
  local flag = true
  for i=1,#EquipHeroScene.equipBKs do
    local rect = ActionUtil.getNodeRect(EquipHeroScene.equipBKs[i]) 
    if rect:containsPoint(ccp(x,y)) then
      print("EquipHeroScene.checkMoveIn ", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
      EquipHeroScene.movesp:setPosition(EquipHeroScene.equipBKs[i]:getPosition())
      EquipHeroScene.movesp=nil
      flag = false
      break
    end
  end
  if flag then
    EquipHeroScene.node:removeChild(EquipHeroScene.movesp, true)
    EquipHeroScene.movesp=nil
  end 
end

function onSelectHero(node, event)
  print("onSelectHero", event, CCControlEventTouchDown)
  EquipHeroScene.node:removeChildByTag(NodeID.HeroImg, true)
  local name = NodeManager:getName(node)
  local sp = CCSprite:create(string.format("%shead.png",name))
  local heroBK = EquipHeroScene.node:getChildByTag(NodeID.HeroBK)
  sp:setPosition(heroBK:getPosition())
  sp:setTag(NodeID.HeroImg)
  EquipHeroScene.node:addChild(sp)
end 

function onSelectEquip(node, event)
  local name = NodeManager:getName(node)
  if event==CCControlEventTouchDragExit then
    print("onSelectEquip begin drag", event)
    local sp=CCSprite:create(string.format("equip_1_%s_1.png",name))
    sp:setPosition(node:getPosition())
    EquipHeroScene.node:addChild(sp)
    EquipHeroScene.movesp = sp
  elseif event==CCControlEventTouchUpOutside then
    if EquipHeroScene.movesp then
      --EquipHeroScene.node:removeChild(EquipHeroScene.movesp, true)
    end
  end
end

function onTouchLayer(event, x, y)
  if event==CCTOUCHBEGAN then
    print("onTouchLayer", event, x, y)
    return true
  elseif event==CCTOUCHMOVED then
    if EquipHeroScene.movesp then
      EquipHeroScene.movesp:setPosition(x-50,y+50) 
    end
  elseif event==CCTOUCHENDED then
    if EquipHeroScene.movesp then
      EquipHeroScene.checkMoveIn(x-50,y+50)
    end
  end
end 
